
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>18.7.&nbsp;小结</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;18&nbsp;章&nbsp;性能优化">
      <link rel="previous" href="string_manipulation.html" title="18.6.&nbsp;优化字符串操作">
      <link rel="next" href="../appendix/furtherreading.html" title="附录&nbsp;A.&nbsp;进一步阅读">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">性能优化</a>&nbsp;&gt;&nbsp;<span class="thispage">小结</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="string_manipulation.html" title="上一页: “优化字符串操作”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="../appendix/furtherreading.html" title="下一页: “进一步阅读”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="soundex.summary"></a>18.7.&nbsp;小结
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>这一章展示了性能优化的几个重要方面，这里是就 <span class="application">Python</span> 而言，但它们却普遍适用。
            </p>
         </div>
         <div class="itemizedlist">
            <ul>
               <li>如果你要在正则表达式和编写循环间抉择，选择正则表达式。正则表达式因其是以 C 语言编译的可以本能地在你的计算机上运行，你的循环却以 <span class="application">Python</span> 编写需要通过 <span class="application">Python</span> 解释器运行。
               </li>
               <li>如果你需要在正则表达式和字符串方法间抉择，选择字符串方法。它们都是以 C 编译的，所以选取简单的。</li>
               <li>字典查找的通常应用很快，但是 <tt class="function">string.maketrans</tt> 之类的特殊函数和 <tt class="methodname">isalpha()</tt> 之类的字符串方法更快。如果 <span class="application">Python</span> 有定制方法给你用，就使它吧！
               </li>
               <li>别太聪明了。有时一些明显的算法是最快的。</li>
               <li>不要太迷恋性能优化，性能并不是一切。</li>
            </ul>
         </div>
         <p>最后一点太重要了，这章中你令这个程序提速三倍并且令百万次的调用节省 20 秒。太棒了！现在思考一下：在那百万次的函数调用中，有多少秒花在周边应用程序等待数据库连接？花在磁盘输入/输出上？花在等待用户输入上？不要在过度优化算法上花时间，从而忽略了其它地方可以做的明显改进。开发你编写运行良好的
            <span class="application">Python</span> 代码的直觉，如果发现明显的失误则修正它们，并不对其它部分过分操作。
         </p>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="string_manipulation.html">&lt;&lt;&nbsp;优化字符串操作</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#soundex.divein" title="18.1.&nbsp;概览">1</a> <span class="divider">|</span> <a href="timeit.html" title="18.2.&nbsp;使用 timeit 模块">2</a> <span class="divider">|</span> <a href="regular_expressions.html" title="18.3.&nbsp;优化正则表达式">3</a> <span class="divider">|</span> <a href="dictionary_lookups.html" title="18.4.&nbsp;优化字典查找">4</a> <span class="divider">|</span> <a href="list_operations.html" title="18.5.&nbsp;优化列表操作">5</a> <span class="divider">|</span> <a href="string_manipulation.html" title="18.6.&nbsp;优化字符串操作">6</a> <span class="divider">|</span> <span class="thispage">7</span>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="../appendix/furtherreading.html">进一步阅读&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>